//
//  ZTBeseSegmentViewController.m
//  zhibo8
//
//  Created by zzt on 16/11/21.
//  Copyright © 2016年 zzt. All rights reserved.
//

#import "ZTBeseSegmentViewController.h"
#define ZTGlobeColor ZTColor(215, 215, 215)
#define ZTColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1]
#define ZTRandomColor ZTColor(arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256))
#define ZTScreenW [UIScreen mainScreen].bounds.size.width
#define ZTScreenH [UIScreen mainScreen].bounds.size.height

static NSString * const ID = @"cell";

@interface ZTBeseSegmentViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, weak) UICollectionView *collectionView;
@property (nonatomic, weak) UISegmentedControl* segment;
@end

@implementation ZTBeseSegmentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    // 添加底部内容view
    [self setupBottomView];
}

#pragma mark - 点击标题
- (void)titleClick:(UISegmentedControl* )segment
{
    _segment = segment;
    NSInteger i = [segment selectedSegmentIndex];
    
    // 2.设置collectionView偏离量
    CGFloat offsetX = i * self.view.frame.size.width;
    
    _collectionView.contentOffset = CGPointMake(offsetX, 0);
//    _collectionView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);

    _segment.selectedSegmentIndex = i;

}

#pragma mark - UICollectionViewDelegate
// 滚动完成的时候就会调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    // 获取页码
    NSInteger page = scrollView.contentOffset.x / self.view.frame.size.width;
    
//    _collectionView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    
    // 选中标题
    _segment.selectedSegmentIndex = page;//一定要默认先点击一下,保证ZTBeseSegmentViewController中先滚动时,_segment不为零
}

#pragma mark - 添加底部内容view
- (void)setupBottomView
{
    // 布局
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = [UIScreen mainScreen].bounds.size;
    // 设置滚动方向
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    
    // UICollectionView
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout];
    collectionView.backgroundColor = ZTGlobeColor;
    [self.view addSubview:collectionView];
    // 不显示水平指示器
    collectionView.showsHorizontalScrollIndicator = NO;
    // 开启分页
    collectionView.pagingEnabled = YES;
    _collectionView = collectionView;
    // 设置collectionView数据源,展示cell,需要把子控制器view添加cell
    collectionView.dataSource = self;
    collectionView.delegate = self;
    // 注册cell
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:ID];
}


#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.childViewControllers.count;
}

// 只要有新的cell出现就会调用
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    
    // 移除其它子控制器view
    [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    // 添加对应的子控制器view 到 对应cell
    UIViewController *vc = self.childViewControllers[indexPath.row];
    
    // 默认控制器frame有y值,每次添加的时候,必须重新去设置子控制器的frame
    vc.view.frame = [UIScreen mainScreen].bounds;
    
    // 添加到contentView
    [cell.contentView addSubview:vc.view];
    
    return cell;
}

@end
